
#include "ElementDao.h"


ElementDao::ElementDao(void) {
	this->db = new map<string,Element*>();
}	

ElementDao::~ElementDao(void){
	map<string,Element *>::iterator iter;
	for(iter = this->db->begin() ; iter != this->db->end() ; ++iter) {
		delete iter->second; // Elimina los objetos de DB.
	}
	delete db; // Elimina DB.
}

void ElementDao::save(Element * object){
	try {
		this->find(object->getId());
		throw PersistenceUniqueConstraintException("Se intento guardar un elemento duplicado.");
	}catch(NoSuchElementException e) {
		this->db->insert(make_pair(object->getId(),object));
	}	
}

Element * ElementDao::find(string id){
	map<string,Element *>::iterator iter = db->find(id);
	if(iter != db->end()) {
		return iter->second;
	}
	throw NoSuchElementException("No existe el elemento con ese id");
}

list<Element *> ElementDao::findAll(void){
	list<Element *> result;
	map<string,Element *>::iterator iter;
	for(iter = this->db->begin() ; iter != this->db->end() ; ++iter) {
		result.push_front(iter->second);
	}
	return result;
}

void ElementDao::deleteObject(Element * objectToDelete){

}

